/*
*
*      Copyright (c)  2011-2013  Ivan Balaban 
*      ivvaan@gmail.com
*
This file is part of Seg_int library.

Seg_int is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Seg_int is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Seg_int.  If not, see <http://www.gnu.org/licenses/>.
*/ 
#include "robust_utils.h"

#define PRIME1 33461
#define PRIME2 23833
#define P1_INV_MOD_P2 12721
#define PRIME3 28657
#define MUL1 1867
#define MUL2 2833
#define MUL3 7657

double randm()
  {
  static int4 f1=MUL1;
  static int4 f2=MUL2;
  static int4 f3=MUL3;
  f1=(f1*MUL1)%PRIME1;
  f2=(f2*MUL2)%PRIME2;
  f3=(f3*MUL3)%PRIME3;
  return (double)f1+(double)f2*200.0+(double)f3*30000.0;
  };

double CRandomValueGen::GetRandomDouble()
  {
  return randm();
  }

BOOL CRandomValueGen::RandomChoose()
  {
  double r=randm();
  return randm()>r;
  }

int4 CRandomValueGen::GetRandomInt(int4 max)//returns integer from 0 to max-1
{
  static int4 f1=MUL1;
  static int4 f2=MUL2;
  f1=(f1*MUL1)%PRIME1;
  f2=(f2*MUL2)%PRIME2;
  int4 numb=f2-f1;
  while(numb<0) numb+=PRIME2;
  numb=(numb*P1_INV_MOD_P2)%PRIME2;
  return (f1+numb*PRIME1)%max;
};

